<?php
require_once(LIB.'/class_Servicedesk.php');
class manage_configuration_controller{
    
    public function exec_index(){
       $content=$this->show_view_index();
       return array('content'=>$content);
    }
    
    public function exec_view(){
       $content=$this->show_view_detail();
       return array('content'=>$content);
    }
    
    public function exec_edit(){
       $config_name=POST_value('config_name','');
       $description=POST_value('description','');
       $device_customer=POST_value('device_customer','');
       $detail=POST_value('detail','');
       
        $status=  GET_value('s','');
        //echo $config_name;
        if(($config_name=="")||($device_customer=="0")||($detail=="")){
            $content = "<div style='margin:1px;padding:1px;background:#eee;border:1px solid #ddd;'> Please fill the require fields (*)</div>";
            $content .= $this->show_edit();   
        }else{
            if ($status=='submit'){
                $message = Servicedesk :: update_configuration();
                //print_r($_FILES['file']);
                $content = "<div style='margin:1px;padding:1px;background:#eee;border:1px solid #ddd;'>".$message."</div>";
                $content .= $this->show_edit();
            }else{
                $content = $this->show_edit();            
            }
        }
       return array('content'=>$content);
    }
    
    private function show_edit(){
       global $dbdefault;

        $qry="SELECT idcustomers, namecustomer FROM customers";
       
       $resqry = mysql_query($qry,$dbdefault) or die(__CLASS__.'->'.__METHOD__.':'.mysql_error());
       $posts=array();
        while($arr = mysql_fetch_assoc($resqry)){
            $posts[] = $arr;            
        }
        
       $c_id =  GET_value('c_id','');
       $query=sprintf("SELECT * FROM configurations WHERE idconfigurations=%d",  qs($c_id));
       
       $res = mysql_query($query,$dbdefault);
       $row = mysql_fetch_array($res); 
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_configuration/view/edit_form.html');
        $posts_tpl ->set('posts', $posts);
        $posts_tpl ->set('row', $row);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_configuration/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }


    private function show_view_detail(){
       global $dbdefault;
       $c_id =  GET_value('c_id','');
       $query=sprintf("SELECT c.*, d.namedevice FROM (SELECT a.*, b.namecustomer FROM configurations a 
           JOIN customers b ON a.`idcustomer`=b.`idcustomers` 
           WHERE a.`idconfigurations`=%d) c LEFT JOIN devices d ON c.iddevice=d.`iddevices`",  qs($c_id));
       
       $res = mysql_query($query,$dbdefault);
       $row = mysql_fetch_array($res);  
       
       $posts_tpl = new Template(MODULE_ROOT.'/manage_configuration/view/view_detail.html');
       $posts_tpl ->set('row', $row);
       $m_content=$posts_tpl->fetch();
       $menu = $this->show_left_menu();

       $tpl = new Template(MODULE_ROOT.'/manage_configuration/view/layout.html');
       $tpl ->set('left_menu', $menu);
       $tpl ->set('content', $m_content);

       return $tpl ->fetch(); 
    }
    
   public function exec_create(){
       //if(isset($_FILES['file'])) print_r($_FILES['file']);
       $config_name=POST_value('config_name','');
       $description=POST_value('description','');
       $device_customer=POST_value('device_customer','');
       $detail=POST_value('detail','');
        $status=  GET_value('s','');
        //echo $config_name;
        if(($config_name=="")||($device_customer=="0")||($detail=="")){
            $content = "<div style='margin:1px;padding:1px;background:#eee;border:1px solid #ddd;'> Please fill the require fields (*)</div>";
            $content .= $this->show_create_form();   
        }else{
            if ($status=='submit'){
                $message = Servicedesk :: create_configuration();
                //print_r($_FILES['file']);
                $content = "<div style='margin:1px;padding:1px;background:#eee;border:1px solid #ddd;'>".$message."</div>";
                $content .= $this->show_create_form();
            }else{
                $content = $this->show_create_form();            
            }
        }

        return array('content'=>$content);

    }
    
    private function show_create_form(){
        
        global $dbdefault;
       // $files=array();
       //if(isset($_FILES['file'])) $files=$_FILES['file'];
        $query="SELECT idcustomers, namecustomer FROM customers";
       
       $res = mysql_query($query,$dbdefault) or die(__CLASS__.'->'.__METHOD__.':'.mysql_error());
       $posts=array();
        while($arr = mysql_fetch_assoc($res)){
            $posts[] = $arr;            
        }
        $posts_tpl = new Template(MODULE_ROOT.'/manage_configuration/view/create_form_general.html');
        $posts_tpl ->set('posts', $posts);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_configuration/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }

    private function show_view_index(){
        global $dbdefault;
        $s =  GET_value('s','');
        $config_type = GET_value('config_type',0);
        $group = GET_value('group',0);     

        
        $item_per_page = 20;        
        $page_num = GET_value('p',1);
        $limit = ($page_num-1)*$item_per_page;
        $offset = $item_per_page+1;
        
        $Where="";

        if($config_type=="1"){
            $Where=" and iddevice='0'";
        }
        if($config_type=="2"){
            $Where=" and iddevice!='0'";
        }
        if($s!=""){
            $Where=$Where." and configname like '%$s%'";
        }
        if($group!="0"){
            $Where.=" and idcustomer='$group'";
        }
        
        $qry="SELECT count(*) AS total FROM configurations where status=1".$Where; 
        
        $res_total=mysql_query($qry,$dbdefault) or die(mysql_error()."<br />".$qry);
        $total=mysql_result($res_total,0,'total');
        $num_pages=ceil($total/$item_per_page);
        if(($config_type=="0")||($config_type=="1")){
            $query="SELECT c.*, d.namedevice FROM(SELECT a.*, b.namecustomer FROM configurations a JOIN customers b 
                ON a.`idcustomer`=b.`idcustomers` ".$Where.") c LEFT JOIN devices d ON c.iddevice=d.iddevices 
                ORDER BY idconfigurations DESC LIMIT $limit, $offset";

        }else{
            $query="SELECT c.*, d.namedevice FROM(SELECT a.*, b.namecustomer FROM configurations a JOIN customers b 
                ON a.`idcustomer`=b.`idcustomers` ".$Where.") c JOIN devices d ON c.iddevice=d.iddevices 
                ORDER BY idconfigurations DESC LIMIT $limit, $offset";
        }

        //echo $query;
        $result = mysql_query($query,$dbdefault) or die(__CLASS__."->".__METHOD__.": ".  mysql_error());
        $posts = array(); 
        while($arr=  mysql_fetch_assoc($result)){
            $posts[]=$arr;
        }
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_configuration/view/view_index.html');
        $posts_tpl ->set('s', $s);
        $posts_tpl ->set('group', $group);
        $posts_tpl ->set('config_type', $config_type);
        $posts_tpl ->set('posts', $posts);
        $posts_tpl->set('page_num', $page_num);
        $posts_tpl->set('num_pages', $num_pages);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_device/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }
    
    
   private function show_left_menu($action=''){
       global $dbdefault;
       $group =  GET_value('group','0');
       $query="SELECT a.*, b.namecustomer FROM (SELECT idcustomer, COUNT(*) AS total FROM configurations GROUP BY idcustomer) a JOIN customers b ON a.idcustomer=b.`idcustomers`"; 
       $result = mysql_query($query,$dbdefault) or die(__CLASS__."->".__METHOD__.": ".  mysql_error());
        $postsl = array(); 
        while($arr=  mysql_fetch_assoc($result)){
            $postsl[]=$arr;
        }
        
        
        if (empty($action)) $action =  GET_value('a','index');
        $tpl = new Template(MODULE_ROOT.'/manage_configuration/view/left_menu.html');
        $tpl->set('a',$action);
        $tpl->set('postsl',$postsl);
        $tpl->set('group',$group);
        return $tpl->fetch();
    }
    
    //----------------------------------------AJAX---------------------------------------------------------
    
   public function ajax_get_devices(){
        global $dbdefault;

        $c_id=GET_value('c_id');
        
        $query3=sprintf("SELECT iddevices, namedevice FROM devices WHERE idcustomer='%d' and status=1",  quote_smart($c_id));
        $device_list=array();
        $result3 = mysql_query($query3,$dbdefault);
        if(!$result3){
            //$content_for_bundle=array(1,2,3,4);
        }else{
            while($arr =  mysql_fetch_assoc($result3)){
                $device_list[]=$arr;
               }

            }
            return array('success'=>TRUE,'content'=>$device_list);
        
    }
    
}
?>
